package com.zhentao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhentao.mapper.ChargeCarBrandMapper;
import com.zhentao.mapper.ChargeCarInfoMapper;
import com.zhentao.mapper.ChargeUserMapper;
import com.zhentao.model.common.dtos.ResponseResult;
import com.zhentao.model.common.enums.AppHttpCodeEnum;
import com.zhentao.model.pojo.car.ChargeCarBrand;
import com.zhentao.model.pojo.car.ChargeCarInfo;
import com.zhentao.model.pojo.par.ChargeParOrders;
import com.zhentao.model.pojo.par.dto.ChargeParOrdersDto;
import com.zhentao.model.pojo.par.param.ChargeOrdersParam;
import com.zhentao.model.pojo.par.vo.ChargePayOrdersVo;
import com.zhentao.model.pojo.user.ChargeUser;
import com.zhentao.service.ChargeCarInfoService;
import com.zhentao.service.ChargeParOrdersService;
import com.zhentao.mapper.ChargeParOrdersMapper;
import com.zhentao.user.ChargeUserThreadUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;


import javax.annotation.Resource;
import java.util.List;

import java.util.List;

/**
 * @author 22115
 * @description 针对表【charge_par_orders】的数据库操作Service实现
 * @createDate 2024-12-31 09:50:13
 */
@Service
public class ChargeParOrdersServiceImpl extends ServiceImpl<ChargeParOrdersMapper, ChargeParOrders>
        implements ChargeParOrdersService {
    @Autowired
    private ChargeCarInfoService carInfoService;

    /*
     * @param param
     * @return com.zhentao.model.common.dtos.ResponseResult
     * @description 查询已经支付的订单
     * @author 振涛教育_小Q
     * @date 2025/1/3 9:10
     */

    @Override
    public ResponseResult payOrdersList(ChargeOrdersParam param) {
        // 判断参数
        if (StringUtils.isEmpty(param)) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        // 获取的当前登录的用户
        ChargeUser user = ChargeUserThreadUtils.getChargeUserThreadLocal();
        if (user == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.CHARGE_USER_DATA_NOT_EXIST);
        }

        // 分页
        Page<ChargeParOrders> page = new Page<>(param.getPageNum(), param.getPageSize());
        this.page(page, Wrappers.<ChargeParOrders>lambdaQuery()
                .eq(ChargeParOrders::getUserId, user.getId())
                .orderByDesc(ChargeParOrders::getCreateTime));
        List<ChargeParOrders> records = page.getRecords();
        for (int i = 0; i < records.size(); i++) {
            ChargeParOrders orders = records.get(i);
            ChargePayOrdersVo ordersVo = new ChargePayOrdersVo();
            BeanUtils.copyProperties(orders, ordersVo);
            // 获取车牌号
            if (orders.getVehicleIdentifyNo() != null) {
                ChargeCarInfo car = carInfoService.getOne(Wrappers.<ChargeCarInfo>lambdaQuery()
                        .eq(ChargeCarInfo::getVehicleIdentifyNo, orders.getVehicleIdentifyNo()));
                ordersVo.setCarNo(car.getCarNo());
            }
        }
        return ResponseResult.okResult(page);
    }

    @Resource
    private ChargeUserMapper chargeUserMapper;
    @Resource
    private ChargeCarInfoMapper chargeCarInfoMapper;
    @Resource
    private ChargeCarBrandMapper chargeCarBrandMapper;





    /**
     * @description  管理端  车辆信息管理列表
     * @author 大毅
     * @param chargeParOrdersDto
     * @return com.zhentao.model.common.dtos.ResponseResult
     */
    @Override
    public ResponseResult chargeParOrdersList(ChargeParOrdersDto chargeParOrdersDto) {
        // 创建分页对象
        Page<ChargeParOrders> page = new Page<>(chargeParOrdersDto.getPageNum(), chargeParOrdersDto.getPageSize());
        // 构建查询条件
        LambdaQueryWrapper<ChargeParOrders> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByDesc(ChargeParOrders::getCreateTime);
        // 执行分页查询
        page(page, wrapper);

        // 遍历分页结果并填充相关数据
        for (ChargeParOrders orders : page.getRecords()) {
            Integer userId = orders.getUserId();
            ChargeUser user = chargeUserMapper.selectById(userId);
            orders.setChargeUser(user);

            String vehicleIdentifyNo = orders.getVehicleIdentifyNo();
            ChargeCarInfo chargeCarInfo = chargeCarInfoMapper.selectOne(Wrappers.<ChargeCarInfo>lambdaQuery().eq(ChargeCarInfo::getVehicleIdentifyNo, vehicleIdentifyNo));
            if (chargeCarInfo != null) {
                orders.setChargeCarInfo(chargeCarInfo);
                orders.setCarNo(chargeCarInfo.getCarNo());
            } else {
                // 处理 chargeCarInfo 为空的情况
                orders.setChargeCarInfo(null);
                orders.setCarNo(null);
            }
        }

        for (ChargeParOrders record : page.getRecords()) {
            Integer carBrandId = record.getChargeCarInfo().getCarBrandId();
            ChargeCarBrand chargeCarBrand = chargeCarBrandMapper.selectById(carBrandId);
            record.getChargeCarInfo().setBrandName(chargeCarBrand.getBrandName());
        }


        return ResponseResult.okResult(page);
    }


    /**
     * @description  管理端  车辆信息管理---删除
     * @author 大毅
     * @param id
     * @return com.zhentao.model.common.dtos.ResponseResult
     */
    @Override
    public ResponseResult chargeParOrdersDelete(Long id) {
        boolean b = this.removeById(id);
        return b? ResponseResult.okResult(AppHttpCodeEnum.SUCCESS) : ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
    }

}




